<?php
	/**
	 * 发送签到奖励到邮件列表
	 */
    class SignInPricedetail extends APIBase {
		
		public $tag = "SignInPricedetail";
    	public $isLogin = true;
		
		public function before() {
			$this->initCacheRedis();
			$this->initMysql();
			return true;
		}
		public function logic(){
			$playerID = $this->uid;	
			$id = $this->param['id'];//第几天的奖励
			$list = $this->cache_redis->hMGet("hsignreward:".$id,array('id','mid','reward'));

			if($list){
				$time = time();
				$mintime = strtotime(date("Y-m-d",time()).' 00:00:00');
				$maxtime = strtotime(date("Y-m-d",time()).' 23:59:59');
				$rewardid = $list['id'];
				$mid = $list['mid'];
				/*
				$result = $this->mysql->query("select * from mail_send where rewardid=$rewardid and uid=$playerID and send_date between $mintime and $maxtime");
				if($result){
					while($row0 = mysql_fetch_assoc($result)){
						$rows0[] = $row0;
					}
				}
				*/
				
				//$this->mysql->query("insert mail_send (uid,mid,is_read,is_receive,send_date,update_date,rewardid,type) values($playerID,$mid,0,0,$time,$time,$rewardid,2)");
				$usermail = $this->cache_redis->keys("hUserEmailList:".$playerID.":*");
				if(empty($usermail)){
					$count = 0;
					$this->cache_redis->hMSet("hUserEmailList:".$playerID.":".$count,array('id'=>1,'uid'=>$playerID,'mid'=>$mid,'readstatus'=>0,'receivestatus'=>0,'date'=>$time,'update_date'=>$time,'rewardid'=>$rewardid,'type'=>2));
				}else{
					$arr = array();
					for($i=0;$i<count($usermail);$i++){
						$row = $this->cache_redis->hMGet("hUserEmailList:".$playerID.":".$i,array('type','rewardid','date'));
						if($row['type']==2 && $row['date']>$mintime && $row['date']<=$maxtime && $row['rewardid']==$rewardid){
							$arr[] = $row;
						}
					}
					if(empty($arr)){
						$count = count($usermail);
						$this->cache_redis->hMSet("hUserEmailList:".$playerID.":".$count,array('id'=>$count+1,'uid'=>$playerID,'mid'=>$mid,'readstatus'=>0,'receivestatus'=>0,'date'=>$time,'update_date'=>$time,'rewardid'=>$rewardid,'type'=>2));
					}else{
						exit;
					}
				}
				$signlist = $this->cache_redis->hMGet("hSign:{$playerID}", array('uid','is_sign','signcount','signdate'));

                if(!$signlist['uid']){
					//$this->mysql->query("insert sign (uid,is_sign,signcount,signdate) values($playerID,1,1,$time)");
					$this->cache_redis->hMSet("hSign:{$playerID}",array('uid'=>$playerID,'is_sign'=>1,'signcount'=>1,'signdate'=>$time));
				}else{
					if((strtotime(date("Y-m-d",$time))-strtotime(date("Y-m-d",$signlist['signdate'])))/24/3600<=1){//判断是连天签到
					    $signcount = $signlist['signcount']+1;
						//$id = $signlist['id'];
						//$this->mysql->query("update sign set is_sign=1,signcount=$signcount,signdate=$time where id=$id and uid=$playerID");
						$this->cache_redis->hMset("hSign:{$playerID}",array('is_sign'=>1,'signcount'=>$signcount,'signdate'=>$time));
					}elseif((strtotime(date("Y-m-d",$time))-strtotime(date("Y-m-d",$signlist['signdate'])))/24/3600>1){//不是连天签到
					    $signcount = 1;
						//$id = $signlist['id'];
						//$this->mysql->query("update sign set is_sign=1,signcount=$signcount,signdate=$time where id=$id and uid=$playerID");
						$this->cache_redis->hMset("hSign:{$playerID}",array('is_sign'=>1,'signcount'=>$signcount,'signdate'=>$time));
					}
				}
				$this->returnData(0);
			}else{
				$this->returnError(301,"数据异常");
			}
			
		}
        public function after() {
			$this->deinitCacheRedis();
		}
    }
?>
